﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TARS.User
{
    public partial class ReviewDeliveryMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            loaddata();
        }
        protected void loaddata()
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                //Load name branchesID 
                Guid branchesID = new Guid(Session["BranchesID"].ToString());
                var branches =db.t_BranchesInfo.Where(x=>x.ID==branchesID);
                 foreach(var bID in branches)
                {
                    lblBranches.Text=bID.Branches_Name.ToString();
                }
               
                //Load name employee
                Guid userID = new Guid(Session["IDUser"].ToString());
                var user = db.v_EmployeeDM.Where(x=>x.UserID==userID);
                foreach(var uID in user)
                {
                    lblEmployee.Text = uID.empName;
                }

                //load date now
                lblDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
                
                //load Delivery Detail from Session
                lblDeliveryFName.Text= Session["DeliveryFName"].ToString();
                lblDeliveryLName.Text= Session["DeliveryLName"].ToString();
                lblDeliveryAddress.Text= Session["DeliveryAddress"].ToString();
                lblDeliveryCity.Text =Session["DeliveryCity"].ToString();
                lblDeliveryState.Text =Session["DeliveryState"].ToString();
                lblDeliveryContactNumber.Text =Session["DeliveryContactNumber"].ToString();
                lblDeliveryZipCode.Text =Session["DeliveryZipCode"].ToString();
                
                
                //load Customer Detail from Session
                lblCustomerFName.Text=Session["CustomerFName"].ToString();
                lblCustomerLName.Text=Session["CustomerLName"].ToString();
                lblCustomerAddress.Text=Session["CustomerAddress"].ToString();
                lblCustomerCity.Text=Session["CustomerCity"].ToString();
                lblCustomerState.Text=Session["CustomerState"].ToString();
                lblCustomerContactNumber.Text=Session["CustomerContactNumber"].ToString();
                lblCustomerZipCode.Text=Session["CustomerZipCode"].ToString();
                lblCustomerEmail.Text=Session["CustomerEmail"].ToString();

                //load Item Detail from Session begin :
            
                //load BranchesZone
                Guid branchesZoneID = new Guid(Session["BranchesZoneID"].ToString());
                var brachesZone = db.t_BrachesZone.Where(x => x.ID == branchesZoneID);
                foreach (var braID in brachesZone)
                {
                    lblBranChesZone.Text = braID.ZoneName.ToString();
                }

                //load  CarrierType
                Guid carrierTypeID = new Guid(Session["CarrierTypeID"].ToString());
                var carrierType = db.t_CarrierType.Where(x => x.ID == carrierTypeID);
                foreach (var carrId in carrierType)
                {
                    lblCarrierType.Text = carrId.CarrierTypeName.ToString();
                }

                //load WeightType , add total money
                Guid weightTypeID = new Guid(Session["WeightTypeID"].ToString());
                var weightType = db.t_WeightType.Where(x => x.ID == weightTypeID);
                foreach (var weightID in weightType)
                {
                    lblWeightType.Text = weightID.WeightTypeName.ToString();
                    if (weightID.PerKg == true)
                    {
                        totalPriceTrue();
                    }else
                        if (weightID.PerKg == false)
                        {
                            totalPriceFalse();
                        }
                }

                //load dropZone
                Guid dropZoneID = new Guid(Session["DropZoneID"].ToString());
                var dropZone = db.t_DropZone.Where(x => x.ID == dropZoneID);
                foreach (var dropID in dropZone)
                {
                    lblDropZone.Text = dropID.DropZoneName.ToString();
                }
                //load anything Else 
                lblInsurance.Text = Session["Insurance"].ToString();
                lblTotalWeight.Text=Session["totalWeight"].ToString();
                lblItemDetail.Text=Session["ItemDetail"].ToString();
                //set SecretNumber 
                Guid secret = Guid.NewGuid();
                lblSecretNumber.Text = secret.ToString();
            }
        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                if (btnSubmit.Text == "Submit")
                {
                    btnSubmit.Text = "Print";
                    btnBack.Text = "Exit";
                    //set part
                    Guid empID = new Guid(Session["IDemployee"].ToString());
                    Guid branchesID = new Guid(Session["BranchesZoneID"].ToString());
                    Guid carrierTypeID = new Guid(Session["CarrierTypeID"].ToString());
                    Guid weightTypeID = new Guid(Session["WeightTypeID"].ToString());
                    Guid priceID = new Guid(Session["PriceID"].ToString());
                    Guid dropZoneID = new Guid(Session["DropZoneID"].ToString());
                    Guid secrestNB = new Guid(lblSecretNumber.Text.ToString());

                    double totalWeight = Convert.ToDouble(lblTotalWeight.Text.ToString());
                    bool insurance = Convert.ToBoolean(Session["Insurance"].ToString());                 
                    DateTime createDate = Convert.ToDateTime(DateTime.Now.ToString());
                    DateTime updateDate = Convert.ToDateTime(DateTime.Now.ToString());
                    decimal totalMoney = Convert.ToDecimal(lblTotalMoney.Text);

                    //insert new Delivery Master 
                    var insertDeliveryMaster = db.Insert_DeliveryMaster(branchesID, empID, carrierTypeID, weightTypeID, priceID, dropZoneID,
                       lblItemDetail.Text, lblDeliveryFName.Text, lblDeliveryLName.Text, lblDeliveryAddress.Text, lblDeliveryCity.Text,
                       lblDeliveryState.Text, lblDeliveryZipCode.Text, lblDeliveryContactNumber.Text, lblCustomerFName.Text,
                       lblCustomerLName.Text, lblCustomerAddress.Text, lblCustomerCity.Text, lblCustomerState.Text, lblCustomerZipCode.Text,
                       lblCustomerContactNumber.Text, lblCustomerEmail.Text, totalWeight, totalMoney, secrestNB, insurance, createDate, updateDate);
                    //insert new Delivery Status
                    var load = db.t_DeliveryMaster.Where(x => x.ScretNumber == secrestNB);
                    foreach (var insMT in load)
                    {
                        var insertDeliveryStatus = db.Insert_DeliveryStatus(insMT.ID, "GO ON", createDate);
                    }
                    //Remove Session Item Detail
                    Session.Remove("BranchesZoneID");
                    Session.Remove("CarrierTypeID");
                    Session.Remove("WeightTypeID");
                    Session.Remove("DropZoneID");
                    Session.Remove("totalWeight");
                    Session.Remove("ItemDetail");
                    Session.Remove("Insurance");
                    Session.Remove("PriceID");
                    //Remove Session Delivery Detail
                    Session.Remove("DeliveryFName");
                    Session.Remove("DeliveryLName");
                    Session.Remove("DeliveryAddress");
                    Session.Remove("DeliveryCity");
                    Session.Remove("DeliveryState");
                    Session.Remove("DeliveryContactNumber");
                    Session.Remove("DeliveryZipCode");

                    //Remove Session Customer Detail
                    Session.Remove("CustomerFName");
                    Session.Remove("CustomerLName");
                    Session.Remove("CustomerAddress");
                    Session.Remove("CustomerCity");
                    Session.Remove("CustomerState");
                    Session.Remove("CustomerContactNumber");
                    Session.Remove("CustomerZipCode");
                    Session.Remove("CustomerEmail");

                    //Set branchesID to insert new delivery
                    Session["BranchesZoneID"] = "";
                }else
                    if (btnSubmit.Text == "Print")
                    {
                        Response.Write("Print");
                        btnSubmit.Text = "Submit";
                        btnBack.Text = "Back";
                    }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (btnBack.Text == "Back")
            {
                btnSubmit.Text = "Submit";
                Response.Redirect("DeliveryMaster.aspx");
            }else
                if (btnBack.Text == "Exit")
                {
                    Response.Write("Out");
                }
        }
        protected void totalPriceTrue()
        {
            int price = 0;
            Guid priceID = new Guid(Session["PriceID"].ToString());
            
            using (var db = new TARSDelivery_EprojectEntities())
            {
                var loadprice = db.t_Prices.Where(x => x.ID == priceID);
                foreach (var a in loadprice)
                {
                    price = Convert.ToInt32(a.Prices);
                }
            }
            lblTotalMoney.Text=(Convert.ToDouble(Session["totalWeight"].ToString())*price).ToString();
           
        }
        protected void totalPriceFalse()
        {
            Guid priceID = new Guid(Session["PriceID"].ToString());
            using (var db = new TARSDelivery_EprojectEntities())
            {
                var loadprice = db.t_Prices.Where(x => x.ID == priceID);
                foreach (var a in loadprice)
                {
                    lblTotalMoney.Text = a.Prices;
                }
            }
        }
    }
}